<?php 
// header("Content-type: text/html; charset=utf-8");


// header("Content-type: image/png");


/**
* 水印类
*/
class Code
{	
	public $config=array(
		'bgColor'=>array(255,255,255),//背景颜色
		'width'=>200,//宽
		'heigth'=>100,//高
		'fontsiez'=>40,//字体大小
		'shu'=>4,//验证码数
	);
	public $src;
	public $color;
	function __construct($config=array())
	{
		if (!empty($config)) {
			$this->config=$config;
		}
	}

	public function entry()
	{   
		
		$this->jianshe();
		$this->line();    
		$this->point();
		$this->text();
		$this->zadian();
		$this->out();
		$this->eee();
	}
	public function jianshe()
	{
		header("Content-type: image/png");
		$this->src = imageCreateTrueColor($this->config['width'],$this->config['heigth']);
		$this->color = imageColorAllocate($this->src,$this->config['bgColor'][0],$this->config['bgColor'][1],$this->config['bgColor'][2]); 
		imageFill($this->src,0,0,$this->color);
	}
	public function line()
	{			//线
		for ($i=0; $i <50 ; $i++) { 
			$this->color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
		imageline ( $this->src ,  mt_rand(0,$this->config['width']) ,mt_rand(0,$this->config['heigth']),mt_rand(0,$this->config['width']), mt_rand(0,$this->config['heigth']), $this->color );
		}
		
	}
	public function point()
	{			//点
		for ($i=0; $i <200 ; $i++) { 
			$this->color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
			imagesetpixel ( $this->src , mt_rand(0,$this->config['width']) , mt_rand(0,$this->config['width']), $this->color );
		}
	}
	public function text()
	{			//文本

		for ($i=0; $i <$this->config['shu']; $i++) { 
			$jianju=$i*($this->config['width']/$this->config['shu'])+($this->config['width']/$this->config['shu']-$this->config['fontsiez'])/2;	
			$hg=($this->config['heigth']-$this->config['fontsiez'])/2+$this->config['fontsiez'];
			$arr="zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789";
			$num=$arr[mt_rand(0,61)];
			$this->color = imageColorAllocate($this->src,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));		//字体 倾斜度
			imagettftext ($this->src , $this->config['fontsiez'] , mt_rand(-30,30) , $jianju , $hg , $this->color ,"./msyh.ttf", $num );
		}		
	}
	public function zadian()
		{
			for ($i=0; $i <40; $i++) { 
				
			$arr="zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789";
			$num=$arr[mt_rand(0,61)];
			$this->color = imageColorAllocate($this->src,mt_rand(125,255),mt_rand(125,255),mt_rand(125,255));		//字体 倾斜度
			imagettftext ($this->src , 12 , mt_rand(-30,30) , mt_rand(0,$this->config['width']) , mt_rand(0,$this->config['heigth']) , $this->color ,"./msyh.ttf", $num );
		}
	}	
	//3输出
	public function out(){
		imagepng($this->src); 
	}
	 
	//4释放资源
	public function eee(){
		imageDestroy($this->src) ;
	}
}
















 ?>